package com.dgshanger.sanhxiaofeisc.ui

import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.ui.login.LoginActivity
import com.xx.baseuilibrary.mvp.BaseMvpViewActivity
import kotlinx.android.synthetic.main.activity_guide.*
import java.util.*


/**
 * GuideActivity
 * 沉迷学习不能自拔
 * Describe：第一次使用时滑动引导页
 * Created by 雷小星🍀 on 2018/4/10 18:38.
 */
class GuideActivity : BaseMvpViewActivity() {
    override fun openSwipeBack(): Boolean {
        return false
    }

    override fun getActivityLayoutId(): Int = R.layout.activity_guide

    override fun initData() {

        val guides = intArrayOf(R.layout.layout_guide_1,
                R.layout.layout_guide_2,
                R.layout.layout_guide_3,
                R.layout.layout_guide_4)
        val guideViews = ArrayList<View>()
        for (layoutId in guides.iterator()) {
            val view = LayoutInflater.from(mContext).inflate(layoutId, null)
            guideViews.add(view)
        }
        viewPager.adapter = object : PagerAdapter() {
            override fun isViewFromObject(view: View, `object`: Any): Boolean {
                return view == `object`
            }

            override fun getCount(): Int = guideViews.size

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                container.addView(guideViews[position])
                return guideViews[position]
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(guideViews[position])
            }
        }

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                //控制显示立即体验按钮
                btn_go.visibility = if (position == guideViews.size - 1) View.VISIBLE else View.GONE
            }

        })
    }

    override fun initEvent() {
        btn_go.setOnClickListener {
            Constants.setNotFirst()//设置为非第一次使用
            startActivityThenFinishSelf(LoginActivity::class.java)
        }
    }
}